
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>手写验证码 | home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="手写验证码
django接口12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364#绘画库from PIL import ImageDraw#图片库from PIL import Image#文件流import io#随机库import randomimport redis#定义ip和端口host = &#34;localhost&#34;port = 6379#建立链接r = redis.Redis(host=host,port=port)#自定义验证码...">
    
        <link rel="icon" href="/favicon.ico">
    
    
        
            <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
        
            <link rel="stylesheet" href="/css/stage.css">
        
            <link rel="stylesheet" href="/css/avatar-bg.css">
        
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body>
<header id="header">
    <div class="menu">
        <i class="fa fa-bars"></i>
    </div>
    <div class="header-main">
        <h1><a href="/">home</a></h1>
    </div>
    <div id="nav">
        <div class="nav-img" id="nav-img"></div>
        <div class="sentences">
            天生我材必有用。——李白
        </div>
    </div>
</header>

<div id="content-outer">
    <div id="content-inner">
        <div class="clearfix">
    <article id="post">
        <h1>手写验证码</h1>
        <div class="create">
            <span>Created</span>
            
                <time datetime="2020-04-24T07:36:10.000Z">
                    2020-04-24
                </time>
            
            
        </div>
        <h2 id="手写验证码"><a href="#手写验证码" class="headerlink" title="手写验证码"></a>手写验证码</h2><hr>
<h3 id="django接口"><a href="#django接口" class="headerlink" title="django接口"></a>django接口</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">#绘画库</span></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> ImageDraw</span><br><span class="line"><span class="comment">#图片库</span></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"><span class="comment">#文件流</span></span><br><span class="line"><span class="keyword">import</span> io</span><br><span class="line"><span class="comment">#随机库</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> redis</span><br><span class="line"></span><br><span class="line"><span class="comment">#定义ip和端口</span></span><br><span class="line">host = <span class="string">"localhost"</span></span><br><span class="line">port = <span class="number">6379</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#建立链接</span></span><br><span class="line">r = redis.Redis(host=host,port=port)</span><br><span class="line"></span><br><span class="line"><span class="comment">#自定义验证码</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyCode</span><span class="params">(View)</span>:</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">#定义随机颜色</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get_random_color</span><span class="params">(self)</span>:</span></span><br><span class="line">        R = random.randrange(<span class="number">255</span>)</span><br><span class="line">        G = random.randrange(<span class="number">255</span>)</span><br><span class="line">        B = random.randrange(<span class="number">255</span>)</span><br><span class="line">        <span class="keyword">return</span> (R,G,B)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#定义图片视图</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get</span><span class="params">(self,request)</span>:</span></span><br><span class="line">        <span class="comment">#画布</span></span><br><span class="line">        img_size = (<span class="number">120</span>,<span class="number">50</span>)</span><br><span class="line">        <span class="comment">#定义图片对象</span></span><br><span class="line">        image = Image.new(<span class="string">'RGB'</span>,img_size,<span class="string">'white'</span>)</span><br><span class="line">        <span class="comment">#定义画笔</span></span><br><span class="line">        draw = ImageDraw.Draw(image,<span class="string">'RGB'</span>)</span><br><span class="line">        source = <span class="string">"0123456789abcdefg"</span></span><br><span class="line">        <span class="comment">#接受容器</span></span><br><span class="line">        code_str = <span class="string">''</span></span><br><span class="line">        <span class="comment">#进入循环绘制</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>):</span><br><span class="line">            <span class="comment">#获取字母颜色</span></span><br><span class="line">            text_color = self.get_random_color()</span><br><span class="line">            <span class="comment">#获取随机下表</span></span><br><span class="line">            tmp_num = random.randrange(len(source))</span><br><span class="line">            <span class="comment">#随机字符串</span></span><br><span class="line">            random_str = source[tmp_num]</span><br><span class="line">            <span class="comment">#装入容器</span></span><br><span class="line">            code_str += random_str</span><br><span class="line">            <span class="comment">#绘制字符串</span></span><br><span class="line">            draw.text((<span class="number">10</span>+<span class="number">30</span>*i,<span class="number">20</span>),random_str,text_color)</span><br><span class="line">        </span><br><span class="line">        <span class="comment">#获取缓存区</span></span><br><span class="line">        buf = io.BytesIO()</span><br><span class="line">        <span class="comment">#将图片存入</span></span><br><span class="line">        image.save(buf,<span class="string">'png'</span>)</span><br><span class="line">        <span class="comment">#保存随机码</span></span><br><span class="line">        r.set(<span class="string">'code'</span>,code_str)</span><br><span class="line">        print(r.get(<span class="string">'code'</span>))</span><br><span class="line">        <span class="comment">#存session</span></span><br><span class="line">        request.session[<span class="string">'code'</span>] = code_str</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> HttpResponse(buf.getvalue(),<span class="string">'image/png'</span>)</span><br></pre></td></tr></table></figure>
<h2 id="取验证码对比"><a href="#取验证码对比" class="headerlink" title="取验证码对比"></a>取验证码对比</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Login</span><span class="params">(APIView)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get</span><span class="params">(self,request)</span>:</span></span><br><span class="line">        <span class="comment">#接收参数</span></span><br><span class="line">        code = request.GET.get(<span class="string">'code'</span>,<span class="literal">None</span>)</span><br><span class="line">        <span class="comment">#redis验证码对比</span></span><br><span class="line">        r_code = r.get(<span class="string">'code'</span>).decode(<span class="string">'utf-8'</span>)</span><br><span class="line">        <span class="keyword">if</span> code != r_code:</span><br><span class="line">            <span class="keyword">return</span> Response(&#123;<span class="string">"code"</span>: <span class="number">403</span>, <span class="string">"message"</span>: <span class="string">"验证码错误"</span>&#125;)</span><br><span class="line">        <span class="comment">#session验证码对比</span></span><br><span class="line">        session_code = request.session.get(<span class="string">'code'</span>,<span class="literal">None</span>)</span><br><span class="line">        <span class="keyword">if</span> code != session_code:</span><br><span class="line">            <span class="keyword">return</span> Response(&#123;<span class="string">"code"</span>: <span class="number">403</span>, <span class="string">"message"</span>: <span class="string">"验证码错误"</span>&#125;)</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="vue展示"><a href="#vue展示" class="headerlink" title="vue展示"></a>vue展示</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    &lt;tr&gt;</span><br><span class="line">        &lt;td&gt;</span><br><span class="line">            验证码：</span><br><span class="line">        &lt;&#x2F;td&gt;</span><br><span class="line">        &lt;td&gt;</span><br><span class="line">            &lt;input type&#x3D;&quot;text&quot; v-model&#x3D;&quot;code&quot;  placeholder&#x3D;&quot;请输入验证码&quot;&gt;</span><br><span class="line">        &lt;&#x2F;td&gt;</span><br><span class="line">        &lt;td&gt;</span><br><span class="line">            &lt;img class&#x3D;&quot;imgcode&quot; :src&#x3D;&quot;yzimg&quot; alt&#x3D;&quot;验证码加载失败&quot; @click&#x3D;&quot;yzm()&quot;&gt;</span><br><span class="line">        &lt;&#x2F;td&gt;</span><br><span class="line">    &lt;&#x2F;tr&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  data () &#123;</span><br><span class="line">      return&#123;</span><br><span class="line">          &#x2F;&#x2F;验证码图片</span><br><span class="line">          yzimg:&#39;http:&#x2F;&#x2F;127.0.0.1:8000&#x2F;code&#x2F;&#39;,</span><br><span class="line">          &#x2F;&#x2F;输入验证码</span><br><span class="line">          code:&#39;&#39;,</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods:&#123;</span><br><span class="line">       &#x2F;&#x2F;验证码刷新</span><br><span class="line">        yzm()&#123;</span><br><span class="line">            var suiji &#x3D; Math.random()</span><br><span class="line">            this.yzimg &#x3D; &#39;http:&#x2F;&#x2F;127.0.0.1:8000&#x2F;code&#x2F;?code&#x3D;&#39;+suiji</span><br><span class="line">        &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">&#x2F;&#x2F;鼠标放置验证码图片时变手形</span><br><span class="line">.imgcode &#123;</span><br><span class="line">    cursor:pointer;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>
        <div>
            <ul class="tags-category-list" itemprop="keywords"><li class="tags-category-list-item"><a class="tags-category-list-link" href="/hexo_blog/tags/python/" rel="tag">python</a></li></ul>
        </div>
        <div class="bottom-line"></div>
        
    <nav id="article-nav">
        
            <a href="/hexo_blog/2020/04/25/%E6%BB%91%E5%8A%A8%E9%AA%8C%E8%AF%81/" id="article-nav-newer" class="article-nav-link-wrap">
        <span class="article-nav-title">
            
                滑动验证
            
        </span>
                <strong class="article-nav-caption">&gt;</strong>
            </a>
        
        
            <a href="/hexo_blog/2020/04/23/day05/" id="article-nav-older" class="article-nav-link-wrap">
                <strong class="article-nav-caption">&lt;</strong>
                <span class="article-nav-title">
                    
                        Postman的基本操作
                </span>
                
            </a>
        
    </nav>


        
    </article>
    <div id="toc">
        
            <h2>Table of Contents</h2>
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#手写验证码"><span class="toc-number">1.</span> <span class="toc-text">手写验证码</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#django接口"><span class="toc-number">1.1.</span> <span class="toc-text">django接口</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#取验证码对比"><span class="toc-number">2.</span> <span class="toc-text">取验证码对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#vue展示"><span class="toc-number">3.</span> <span class="toc-text">vue展示</span></a></li></ol>
        
    </div>
</div>

    </div>
</div>
<footer id="footer">
    <div id="copyright">&copy; Mr.Song  2020</div>
    <div id="theme">
        Powered by <a href="http://hexo.io" target="_blank" rel="noopener">Hexo</a>. Theme by <a href="https://github.com/markyong/hexo-theme-stage" target="_blank" rel="noopener">Stage</a>
    </div>
</footer>
<script src="/lib/js/waterrippleeffect.min.js"></script>
<script src="/js/header-bg.main.js"></script>

    <script src="/lib/js/smooth-scroll.min.js"></script>
    <script src="/js/toc.main.js"></script>

</body>
</html>
